#include "LayerStack.h"


namespace Hazel{
    LayerStack::LayerStack() {
//        m_LayerInsertIndex = m_Layers.begin();
    }

    LayerStack::~LayerStack() {
        for(Layer* layer : m_Layers){
            layer->OnDetach();
            delete layer;
        }
    }


    void LayerStack::PushLayer(Layer *layer) {
        //emplace：在指定位置直接构造元素。
        m_Layers.emplace(m_Layers.begin() + m_LayerInsertIndex, layer);
        m_LayerInsertIndex++;
//        layer->OnAttach();
    }

    void LayerStack::PushOverlay(Layer *overlay) {
        //emplace_back：在容器末尾直接构造元素。
        m_Layers.emplace_back(overlay);
//        overlay->OnAttach();
    }

    void LayerStack::PopLayer(Layer *layer) {
        //std::find 查找元素，如果找不到就返回最后一个end
        //erase 移除单个元素
        auto it = std::find(m_Layers.begin(), m_Layers.begin() + m_LayerInsertIndex, layer);
        if (it != m_Layers.end()) {
            layer->OnDetach();
            m_Layers.erase(it);
            m_LayerInsertIndex--;
        }
    }

    void LayerStack::PopOverlay(Layer *overlay) {
        auto it = std::find(m_Layers.begin() + m_LayerInsertIndex, m_Layers.end(), overlay);
        if (it != m_Layers.end()) {
            overlay->OnDetach();
            m_Layers.erase(it);
        }
    }

}